package com.sky.service.impl;


import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import cn.mybatis.mp.core.sql.executor.chain.QueryChain;
import com.sky.constant.MessageConstant;
import com.sky.dto.GetCategoriesDTO;
import com.sky.entity.Bookcategories;
import com.sky.entity.Categories;
import com.sky.exception.BaseException;
import com.sky.mapper.BookcategoriesMapper;
import com.sky.mapper.CategoriesMapper;
import com.sky.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author majun
 * @package com.sky.service.impl
 * @date 2024/12/1 20:35
 */
@Service
public class CategoriesServiceImpl implements CategoriesService {

    @Autowired
    private CategoriesMapper categoriesMapper;

    @Autowired
    private BookcategoriesMapper bookcategoriesMapper;

    /**
     * 获取图书分类信息
     *
     * @return 图书分类
     */
    public List<Categories> getCategories() {

        return QueryChain.of(categoriesMapper).list();

    }

    /**
     * 根据分类名称获取分类数据
     *
     * @param categoriesName 分类名称
     * @return Categories
     */
    public Categories getCategoriesByName(String categoriesName) {

        return categoriesMapper.get(where -> {
            where.eq(Categories::getCategoryName, categoriesName);
        });

    }

    /**
     * 分页获取分类列表
     *
     * @param getCategoriesDTO 分页相关参数
     * @return 分页后的分类列表
     */
    public Pager<Categories> getCateoriesPage(GetCategoriesDTO getCategoriesDTO) {

        return QueryChain.of(categoriesMapper)
                .ignoreNullValueInCondition(true)
                .ignoreEmptyInCondition(true)
                .trimStringInCondition(true)
                .orderBy(Categories::getCategoryId) // 添加根据ID排序的条件
                .like(Categories::getCategoryName, getCategoriesDTO.getCategoryName())
                .paging(Pager.of(getCategoriesDTO.getPageNumber(), getCategoriesDTO.getPageSize()));

    }

    /**
     * 分类信息修改
     *
     * @param categories 分类信息
     */
    public void upCategory(Categories categories) {

        if (categories == null) {
            throw new BaseException(MessageConstant.THE_SUBMISSION_CANNOT_BE_EMPTY);
        }

        categoriesMapper.update(categories);

    }

    /**
     * 新增分类信息
     *
     * @param categories 分类信息
     */
    public void saveCategory(Categories categories) {

        if (categories == null) {
            throw new BaseException(MessageConstant.THE_SUBMISSION_CANNOT_BE_EMPTY);
        }

        categoriesMapper.save(categories);

    }

    /**
     * 删除分类
     *
     * @param categoryId 分类ID
     */
    public void deleteCategory(Integer categoryId) {

        //判断categoryId是否为空
        if (categoryId == null) {
            throw new BaseException(MessageConstant.THE_SUBMISSION_CANNOT_BE_EMPTY);
        }

        //查询是否有关联图书
        List<Bookcategories> bookcategoriesList = bookcategoriesMapper.list(where -> {
            where.eq(Bookcategories::getCategoryId, categoryId);
        });

        //如果有值，则说明有图书关联不允许删除
        if (!bookcategoriesList.isEmpty()) {
            throw new BaseException(MessageConstant.THIS_CATEGORY_HAS_ASSOCIATED_BOOKS);
        }

        //删除分类
        categoriesMapper.delete(where -> {
            where.eq(Categories::getCategoryId, categoryId);
        });

    }
}
